package com.bw.inspection.page

import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.TimeUtils
import com.bw.base.path.RouterPath
import com.bw.common.view.BaseMvvmActivity
import com.bw.inspection.R
import com.bw.inspection.adapter.DateAdapter
import com.bw.inspection.adapter.TaskAdapter
import com.bw.inspection.databinding.ActivityNoInspectionBinding
import com.bw.inspection.entity.DateEntity
import com.bw.inspection.vm.InspectionViewModel
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat
import java.util.Calendar

@Route(path = RouterPath.PATH_INSPECTION_NO_PATH)
@AndroidEntryPoint
class NoInspectionActivity : BaseMvvmActivity<ActivityNoInspectionBinding>() {
    private val mViewModel:InspectionViewModel by viewModels()
    val dateAdapter= DateAdapter()
    val taskAdapter= TaskAdapter()
    override fun getLayoutId(): Int =R.layout.activity_no_inspection

    override fun initViews() {
        super.initViews()
        mViewModel.getNoInspectionData("0")
        var calendar = Calendar.getInstance()
        val week = calendar.get(Calendar.DAY_OF_WEEK)
        mDataBinding.taskDate.setText(SimpleDateFormat("yyyy-MM").format(System.currentTimeMillis()))
        val list = mutableListOf<DateEntity>()
        for (i in -(week-1)..-(week-1)+6){
            calendar = Calendar.getInstance()
            calendar.add(Calendar.DAY_OF_MONTH,i)
            val day = calendar.get(Calendar.DAY_OF_MONTH)
            val week1 = TimeUtils.getChineseWeek(calendar.time).substring(1,2)
            list.add(DateEntity(week1,day,if(i==0) true else false))
        }

        Log.i("===",list.toString())
        dateAdapter.addData(list)
        mDataBinding.taskRvdate.apply {
            adapter=dateAdapter
            layoutManager= GridLayoutManager(this@NoInspectionActivity,7)
        }

        mDataBinding.taskRv.apply {
            adapter=taskAdapter
            layoutManager= LinearLayoutManager(this@NoInspectionActivity)
        }

        taskAdapter.setOnItemClickListener { adapter, view, position ->
           ARouter.getInstance().build(RouterPath.PATH_DETAIL_NO_INSPECTION_PATH)
               .withString("tid",taskAdapter.data.get(position).id.toString())
               .navigation()
        }

    }

    override fun loadData() {
        super.loadData()
        mViewModel.mPatrolSuccess.observe(this){
            taskAdapter.data.clear()
            taskAdapter.data.addAll(it)
            taskAdapter.notifyDataSetChanged()
        }
    }


}